package site.wtcc.originalfavor.func.utils;


import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternUtil {

    private static final Pattern isMobilePattern = Pattern.compile("^[1][0-9]{10}$");
    private static final Pattern isHKPhonePattern = Pattern.compile("^(4|5|6|7|8|9)\\d{7}$");

    /**
     * 手机号验证
     *
     * @param str
     * @return 验证通过返回true
     */
    public static boolean isMobile(String str) {
        Matcher m = isMobilePattern.matcher(str);
        return m.matches();
    }

    /**
     * 是否是香港电话号码
     *
     * @param str
     * @return
     */
    public static boolean isHKPhone(String str) {
        Matcher m = isHKPhonePattern.matcher(str);
        return m.matches();
    }

    /**
     * 手机 固话 星号 加密
     *
     * @param str
     * @return
     */
    public static String phoneEncrypt(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        str = str.replaceAll("([1])(\\d{2})(\\d{5})(\\d{3})", "$1$2*****$4");
        str = str.replaceAll("(\\d{3,4})-(\\d{7,8})", "***-********");
        return str;
    }

    /**
     * 获取最新的电话（去除0，- 中划线）
     *
     * @param tel
     * @return
     */
    public static String getNewTel(String tel) {
        if (StringUtils.isBlank(tel)) {
            return null;
        }
        String customerTel = StringUtils.replace(tel, "-", "");
        if (customerTel != null && customerTel.startsWith("01") && customerTel.length() == 12) {
            customerTel = StringUtils.substring(customerTel, 1, 12);
        }
        return customerTel;
    }
}
